package com.key.service.impl;

import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.dto.AssetsAddDTO;
import com.key.common.dto.AssetsFindDTO;
import com.key.common.vo.AssetsVO;
import com.key.entity.Assets;
import com.key.mapper.AssetsMapper;
import com.key.mapper.MaintenanceInfoMapper;
import com.key.service.AssetsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wei
 * @create 2023-02-20 9:06
 */
@Service
public class AssetsServiceImpl implements AssetsService {

    @Autowired
    private AssetsMapper assetsMapper;

    @Override
    public Map<String, Object> findAssetsList(AssetsFindDTO assetsFindDTO) {

        Map<String, Object> resMap = new HashMap<>();

        List<AssetsVO> assetsVOList;

        if(assetsFindDTO.getCurrentPage() != null && assetsFindDTO.getPageSize() != null){
            //分页
            PageHelper.startPage(assetsFindDTO.getCurrentPage(),assetsFindDTO.getPageSize());
            assetsVOList = assetsMapper.selectAssetsList(assetsFindDTO);
        }else{
            //不分页
            assetsVOList = assetsMapper.selectAssetsList(assetsFindDTO);
        }

        //查询资产总数
        Integer count = assetsMapper.getAssetsCount(assetsFindDTO);

        //查询资产总金额
        Integer moneySum = assetsMapper.getSumMoney(assetsFindDTO);

        //若查询有结果
        if(assetsVOList.size() > 0){

            //取出结果集的所用资产id
            List<Long> assetsIds = new ArrayList<>();
            for(AssetsVO assetsVO : assetsVOList){
                assetsIds.add(assetsVO.getAssetsId());
            }

            //根据资产id查询创建者
            List<Map<String, Object>> assetsCreaterList = assetsMapper.getAssetsCreater(assetsIds);
            //把创建者赋值给资产视图对象的创建者
            for(AssetsVO assetsVO : assetsVOList){
                for(Map<String, Object> map : assetsCreaterList){
                    if(assetsVO.getAssetsId().equals((Long)map.get("assets_id"))){
                        assetsVO.setAssetsCreater((String)map.get("employeeName"));
                    }
                }
            }

            //根据资产id查询所属公司
            List<Map<String, Object>> assetsAdminList = assetsMapper.getAssetsAdmin(assetsIds);
            //把所属公司赋值给资产视图对象的所属公司
            for(AssetsVO assetsVO : assetsVOList){
                for(Map<String, Object> map : assetsAdminList){
                    if(assetsVO.getAssetsId().equals((Long)map.get("assets_id"))){
                        assetsVO.setAssetsAdmin((String)map.get("companyName"));
                    }
                }
            }

            //根据资产id查询管理员
            List<Map<String, Object>> assetsManagerList = assetsMapper.getAssetsManager(assetsIds);
            //把管理员赋值给资产视图对象的管理员
            for(AssetsVO assetsVO : assetsVOList){
                for(Map<String, Object> map : assetsManagerList){
                    if(assetsVO.getAssetsId().equals((Long)map.get("assets_id"))){
                        assetsVO.setAssetsManager((String)map.get("employeeName"));
                    }
                }
            }

            //高级搜索
            //如果所属公司不为null
            if(assetsFindDTO.getAssetsAdmin() != null){
                
                //移除视图对象list里所属公司为null的
                for(AssetsVO assetsVO : assetsVOList){
                    if(!assetsFindDTO.getAssetsAdmin().equals(assetsVO.getAssetsAdmin())){
                        assetsVOList.remove(assetsVO);
                    }
                }
                
                count -= assetsMapper.getAssetsAdminCount(assetsFindDTO.getAssetsAdmin());
                moneySum -= assetsMapper.getAssetsAdminSumMoney(assetsFindDTO.getAssetsAdmin());
            }
            //如果管理员不为null
            if(assetsFindDTO.getAssetsManager() != null){

                //移除视图对象list里管理员为null的
                for(AssetsVO assetsVO : assetsVOList){
                    if(!assetsFindDTO.getAssetsManager().equals(assetsVO.getEmployeeName())){
                        assetsVOList.remove(assetsVO);
                    }
                }
                
                count -= assetsMapper.getAssetsManagerCount(assetsFindDTO.getAssetsManager());
                moneySum -= assetsMapper.getAssetsManagerSumMoney(assetsFindDTO.getAssetsManager());
            }
        }

        
        resMap.put("count", count);
        resMap.put("moneySum", moneySum);
        resMap.put("assetsVOList", assetsVOList);
        return resMap;
    }

    @Override
    public R findAssetsListByAssetClassAndAssetsState(String assetTypeName) {
        R r=new R();

        List<Assets> resultList= assetsMapper.selectAssetsListByAssetClassAndAssetsState(assetTypeName);
        if(resultList!=null){
            r.setCode(200);
            r.setMsg("若没有数据则该条件下没有资产");
            r.setData(resultList);
        }else {
            r.setCode(500);
            r.setMsg("出错了");
        }
        return r;
    }

    @Override
    public boolean removeAssets(List<Long> assetsIds) {
        int i = assetsMapper.deleteAssets(assetsIds);
        return i > 0;
    }

    @Override
    public boolean editAssets(Assets assets) {
        int i = assetsMapper.updateAssets(assets);
        return i > 0;
    }

    @Override
    public boolean addAssets(Assets assets) {
        int i = assetsMapper.addAssets(assets);
        return i > 0;
    }

    @Override
    @Transactional
    public boolean copyAssets(Long assetsId, Integer number) {
        int i = 0;
        try{
            for(int j = 0; j < number; j++){
                i = assetsMapper.copyAssets(assetsId);
            }
        }catch (Exception e){
            i = 0;
        }
        return i > 0;
    }

    @Override
    public Integer getAssetsCount(AssetsFindDTO assetsFindDTO) {
        return assetsMapper.getAssetsCount(assetsFindDTO);
    }
}
